limsFilemanagement.searchPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.fileGridId,
            url: limsFilemanagement.baseUrl + '/queries',
            columns: limsFilemanagement.getSearchListColumns(),
            onCellClick: function (field, row) {
                field === 'fileCode' && limsFilemanagement.loadFileManagementDetailPage(row, 'search')
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    // _this.refreshTabs(null)
                }
            },
            onRowActive: function (index, row) {
                // _this.refreshTabs(row)
            },
            page: true,
            requestData: {
                personFilter: workspace.user.id,
                versonActiveFilter: '1'
            },
        }
    },

    refreshTabs: function (row) {
        var _this = this
        Gikam.getAsyncComp(_this.idManager.fileVersionId).done(grid => {
            if (row) {
                grid.refresh({
                    url: limsFilemanagementVersion.baseUrl + '/queries',
                    requestData: {
                        fileId_EQ: row.id
                    }
                })
            } else {
                grid.refresh({
                    url: limsFilemanagementVersion.baseUrl + '/queries',
                    requestData: {
                        fileId_EQ: -1
                    }
                })
            }
        })
    },

    getFileVersionGrid: function (gridId) {
        return {
            type: 'grid',
            id: gridId,
            // url: limsFilemanagementVersion.baseUrl + '/queries/raw',
            service: 'limsFilemanagementVersionServiceImpl',
            dbTable: 'T_LIMS_FILEMANAGEMENT_VERSION',
            columns: limsFilemanagementVersion.getSearchListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            requestData: {
                versonActiveFilter: '1'
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: []
        }
    },

    getAttachmantStruct: function (gridId, dbTable) {
        return {
            type: 'uploader',
            immediateUpload: true,
            id: gridId + '-uploader',
            dbTable: dbTable,
            // bizId: -1,
            filter: false,
            readonly: true,
            genericQuery: false,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-filemanagement-search-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                title: 'BASEMODULE.RM.FILEMANAGEMENTS.TAB.FILEMANAGEMENT_ITEM',
                items: [this.getGridParam()]
            }
            /*            west: {
                            width: '35%',
                            title: 'BASEMODULE.RM.FILEMANAGEMENTS.TAB.FILEMANAGEMENT_ITEM',
                            items: [this.getGridParam()]
                        },
                        center: {
                            items: [{
                                type: 'layout',
                                north: {
                                    height: '55%',
                                    items: [_this.getFileVersionGrid(_this.idManager.fileVersionId)],
                                    title: 'BASEMODULE.RM.FILEMANAGEMENTS.TAB.FILEMANAGEMENTVERSION_ITEM',
                                },
                                center: {
                                    items: [_this.getAttachmantStruct(_this.idManager.fileVersionId, 'T_LIMS_FILEMANAGEMENT_VERSION')]
                                }
                            }]
                        }*/
        })
    },

    idManager: {
        fileGridId: 'lims-filemanagement-search-list-grid',
        fileVersionId: 'lims-filemanagement-version-search-list-grid',
    },

    init: function () {
        this.create()
    }
}
